<template>
	<!-- search 搜索框 -->
	<my-search bgcolor="#C00000" redius="18" @myclick="goSearch"></my-search>
	<view class="scroll-view-container">
		<!-- 左侧的滚动区域 -->
		<scroll-view scroll-y="true" :style="{height:vh+'px'}" class="left-scroll-view" >
			<view class="left-scroll-view-item" v-for="(item,index) in cateList" :key="index" :class="{'active':active===index}" @click="handleActive(index)">{{item.cat_name}}</view>
		</scroll-view>
		<!-- 右侧的滚动区域 --> 
		<scroll-view class="right-scroll-view" scroll-y :style="{height: vh + 'px'}" :scroll-top="scrollTop">
			<!-- 二级分类列表数据 -->
		    <view  v-for="(item2,index) in cateLevel2" :key="index">
				<view class="right-scroll-view-item">{{item2.cat_name}}</view>
				<!-- 三级分类列表数据 -->
				<view class="right-leve3-container">
					<view v-for="(leve,i) in item2 .children" :key="i" class="right-leve3-item" @click="goGoodsList(leve)">
						<image :src="leve.cat_icon"></image>
						<text>{{leve.cat_name}}</text>
					</view>
				</view>	
			</view>
			
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				vh:0,
				cateList:[],
				 // 二级分类列表
				cateLevel2: [],
				active:0,
				scrollTop: 0
			};
		},
		onLoad(){
			const sysInfo = uni.getSystemInfoSync()
			//windowHeight 是可使用高度（去掉 toptab 和 tabber 剩下的高度)
			this.vh = sysInfo.windowHeight - 50
			this.getCateList()
		},
		methods:{
			async getCateList(){
				 const {data:res} = await uni.$http.get('/api/public/v1/categories')
				 if(res.meta.status !== 200)return uni.$showMsg()
				 this.cateList = res.message
				 this.cateLevel2 = this.cateList[this.active].children
			},
			handleActive(index){ 
				this.active = index
				this.cateLevel2 = this.cateList[index].children
				//为什么要写 1 是因为每次赋值不同重复，重复就不起效果
				this.scrollTop = this.scrollTop?0:1
			},
			goGoodsList(value){
				uni.navigateTo({
					url:'/subpkg/goods_list/goods_list?cid='+value.cat_id
				})
			},
			goSearch(){
				uni.navigateTo({
					url:'/subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss">
.scroll-view-container{
	display:flex;
	.left-scroll-view{
		width:120px;
		// background-color: yellow;
		.left-scroll-view-item{
			line-height:60px;
			height:60px;
			background-color: #f7f7f7;
			text-align: center;
			font-size:12px;
			&.active{
				background-color: #ffffff;
				color:red;
				position: relative;
				&::before{
					content:'';
					display:block;
					width:3px;
					height:30px;
					background-color: #c00000;
					position:absolute;
					left:0;
					top:50%;
					transform:translateY(-50%);
				}
			}
		}
		
	}
}

.right-scroll-view-item{
	font-size:20px;
	font-weight:bold;
	text-align:center;
	padding:15px 0;
}
.right-leve3-container{
	display: flex;
	flex-wrap:wrap;
	.right-leve3-item{
		width:33.33%;
		height:100px; 
		display: flex;
		flex-direction: column;
		align-items: center;
		font-size:12px;
		padding:10px;
		box-sizing: border-box;
		image{
			width:100%;
			height:100%;
		}
	}
}
</style>
